package com.zlsy.secondtime.threadcoreknowledge.threadobjectclasscommonmethods;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author zhouliang
 * @date 2020/5/18 8:45
 * 演示sleep不会释放lock锁
 */
public class SleepDontReleaseLock implements Runnable {

    private static final Lock LOCK = new ReentrantLock();

    @Override
    public void run() {
        LOCK.lock();
        try {
            System.out.println("线程："+Thread.currentThread().getName()+" 获取到了锁.");
            Thread.sleep(1000);
            System.out.println("线程："+Thread.currentThread().getName()+" 已经苏醒");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            LOCK.unlock();
        }
    }

    public static void main(String[] args) {
        SleepDontReleaseLock r = new SleepDontReleaseLock();
        Thread a = new Thread(r);
        Thread b = new Thread(r);

        a.start();
        b.start();
    }
}
